********************************************************************************
** Do file: rl2_takeupbywtp.do
** First started: September 6, 2020 
** Last edited: September 8, 2023 

/* Purpose: this do file plots loan take-up to willingness to pay reported in survey

*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

ssc install blindschemes
set scheme plottig

*** Preamble - make data sets ***
di "$merged"
use "$merged/key_rep.dta", clear


* Generate indicators of take-up at endline
g takeup_end = .
replace takeup_end = 0 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & ///
	k_surveyed==1 & k_surveyed_end==1
replace takeup_end = 1 if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_tookloan_repay==1 & ///
	k_surveyed==1 & k_surveyed_end==1
	
* Keep only the main analysis group
keep if treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U"

* Merge in other variables
merge 1:1 hhid using "$bsvy_clean/hhvars_baseline.dta"
drop if _merge==2
drop _merge
merge 1:1 hhid using "$esvy_clean/9_lockedoccurences_hh.dta"
drop if _merge==2
drop _merge


keep if treatmenttype_sh == "R T1-L" | treatmenttype_sh == "R T2-U"

keep if takeup_end != .

*******************************
***** Part I: Plot by WTP *****
*******************************

* Divide WTPday into three groups
gen wtpday_group = .
replace wtpday_group = 100 if wtpday == 0 | wtpday == 1000
replace wtpday_group = 200 if wtpday == 2000 | wtpday == 3000
replace wtpday_group = 300 if wtpday == 4000 | wtpday == 5000

* Scale to look better in graph
gen wtpday_scaled = wtpday_group / 100 * 4 + 2


* Separate, without surprise unlocked
preserve
count if wtpday != .
local size = r(N)

count if wtpday_group == 100
local size_100 = r(N)
sum takeup_end if treatmenttype_sh == "R T1-L" & wtpday_group == 100
local mean_T1L_100 = r(mean)
sum takeup_end if treatmenttype_sh == "R T2-U" & wtpday_group == 100
local mean_T2U_100 = r(mean)
local diff_100 = `mean_T1L_100' - `mean_T2U_100'
ttest takeup_end if wtpday_group == 100, by(treatmenttype_sh)
local se_100 = r(se)
local hitakeup_100 = `diff_100' + invttail(`size_100' - 2,0.025) * `se_100'
local lotakeup_100 = `diff_100' - invttail(`size_100' - 2,0.025) * `se_100'

count if wtpday_group == 200
local size_200 = r(N)
sum takeup_end if treatmenttype_sh == "R T1-L" & wtpday_group == 200
local mean_T1L_200 = r(mean)
sum takeup_end if treatmenttype_sh == "R T2-U" & wtpday_group == 200
local mean_T2U_200 = r(mean)
local diff_200 = `mean_T1L_200' - `mean_T2U_200'
ttest takeup_end if wtpday_group == 200, by(treatmenttype_sh)
local se_200 = r(se)
local hitakeup_200 = `diff_200' + invttail(`size_200' - 2,0.025) * `se_200'
local lotakeup_200 = `diff_200' - invttail(`size_200' - 2,0.025) * `se_200'

count if wtpday_group == 300
local size_300 = r(N)
sum takeup_end if treatmenttype_sh == "R T1-L" & wtpday_group == 300
local mean_T1L_300 = r(mean)
sum takeup_end if treatmenttype_sh == "R T2-U" & wtpday_group == 300
local mean_T2U_300 = r(mean)
local diff_300 = `mean_T1L_300' - `mean_T2U_300'
di `mean_T1L_300'
di `mean_T2U_300'
di `diff_300'
ttest takeup_end if wtpday_group == 300, by(treatmenttype_sh)
local se_300 = r(se)
local hitakeup_300 = `diff_300' + invttail(`size_300' - 2,0.025) * `se_300'
local lotakeup_300 = `diff_300' - invttail(`size_300' - 2,0.025) * `se_300'
di `size_200'
clear
set obs 3
gen takeup = .
replace takeup = `diff_100' if _n == 1
replace takeup = `diff_200' if _n == 2
replace takeup = `diff_300' if _n == 3
gen wtpday_scaled = .
replace wtpday_scaled = 6 if _n == 1
replace wtpday_scaled = 10 if _n == 2
replace wtpday_scaled = 14 if _n == 3
gen hitakeup = .
replace hitakeup = `hitakeup_100' if _n == 1
replace hitakeup = `hitakeup_200' if _n == 2
replace hitakeup = `hitakeup_300' if _n == 3
gen lotakeup = .
replace lotakeup = `lotakeup_100' if _n == 1
replace lotakeup = `lotakeup_200' if _n == 2
replace lotakeup = `lotakeup_300' if _n == 3

graph twoway (bar takeup wtpday_scaled, color(blue)) (rcap hitakeup lotakeup wtpday_scaled), by() ///
xlabel(6 "0 - 1" 10 "2 - 3" 14 "4 - 5") xtitle("WTP to unlock next day (UGX, thousands)", size(smedium)) ytitle("Difference in Loan Take-up" "Secured over Unsecured", size(smedium)) legend(off,) xsize(10) ysize(10) ///
note("N = `size'", size(smedium) pos(6))
graph export ../figures/takeupbywtp_dif.png, replace

restore

